<%@ page import="br.com.rosena.model.bean.persistente.Persistente" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="br.com.rosena.model.bean.persistente.Atributo" %>
<%@ page import="br.com.rosena.model.bean.persistente.Classe" %>
<%@ page import="br.com.rosena.model.bean.persistente.Metodo" %>
<%
    String nomeClasse = request.getParameter("nomeClasse") != null ? request.getParameter("nomeClasse") : "";
    String strJson = request.getParameter("toStringJson") != null ? request.getParameter("toStringJson") : "";
    Persistente objPersistente = new Persistente(nomeClasse);

    try {
        JSONObject objJSONPersistente = new JSONObject(strJson);

        if (!objJSONPersistente.getString("id").equals("0")) {
            objPersistente = new Persistente(nomeClasse, new Long(objJSONPersistente.getString("id")).longValue());
        }
        Classe objClasse = objPersistente.getClasse();
        Atributo lstAtributo[] = objClasse.getArrayAtributos();

        for (int i = 0; i < lstAtributo.length; i++) {
            Atributo atributo = lstAtributo[i];
            String tipoAtributo = atributo.getTipo();
            if (tipoAtributo.equals(Atributo.String)) {
                objPersistente.setString(atributo.getNome(), objJSONPersistente.getString(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Double)) {
                objPersistente.setDouble(atributo.getNome(), br.com.rosena.util.Util.parseDouble(objJSONPersistente.getString(atributo.getNome()), "0.00"));
            } else if (tipoAtributo.equals(Atributo.Integer)) {
                objPersistente.setInt(atributo.getNome(), objJSONPersistente.getInt(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Boolean)) {
                objPersistente.setBoolean(atributo.getNome(), objJSONPersistente.getBoolean(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Time)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()), "HH:mm");
                objPersistente.setTime(atributo.getNome(), new java.sql.Time(dt.getTime()));
            } else if (tipoAtributo.equals(Atributo.Date)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()), "dd/MM/yyyy");
                objPersistente.setDate(atributo.getNome(), new java.sql.Date(dt.getTime()));
            } else if (tipoAtributo.equals(Atributo.TimeStamp)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()), "HH:mm:ss");
                objPersistente.setTime(atributo.getNome(), new java.sql.Time(dt.getTime()));
            } else if (tipoAtributo.contains(Atributo.XML)) {
                if (!atributo.getConsultaDoAtributo().equals(Atributo.XML_LIST)) {
                    if (objJSONPersistente.getLong(atributo.getNome()) > 0) {
                        objPersistente.setPersistente(atributo.getNome(), new Persistente(atributo.getTipo().replace(".xml", ""), objJSONPersistente.getLong(atributo.getNome())));
                    }
                }
            }
        }

        objPersistente.setString("arquivo","arquivo");
        objPersistente.invocarMetodo("Salvar");//invocar comando
        if (objPersistente.getId() > 0) {
            objJSONPersistente.put("id", objPersistente.getId());
        } else {
            objJSONPersistente.put("id", 0);
        }
        objJSONPersistente.put("error", "");
    } catch (Exception ex) {
        ex.printStackTrace();

        JSONObject objJSONPersistente = new JSONObject(strJson);
        if (!objJSONPersistente.getString("id").equals("0")) {
            objPersistente = new Persistente(nomeClasse, new Long(objJSONPersistente.getString("id")).longValue());
        }
        Classe objClasse = objPersistente.getClasse();
        Atributo lstAtributo[] = objClasse.getArrayAtributos();

        for (int i = 0; i < lstAtributo.length; i++) {
            Atributo atributo = lstAtributo[i];
            String tipoAtributo = atributo.getTipo();
            if (tipoAtributo.equals(Atributo.String)) {
                objPersistente.setString(atributo.getNome(), objJSONPersistente.getString(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Double)) {
                objPersistente.setDouble(atributo.getNome(), br.com.rosena.util.Util.parseDouble(objJSONPersistente.getString(atributo.getNome()), "0.00"));
            } else if (tipoAtributo.equals(Atributo.Integer)) {
                objPersistente.setInt(atributo.getNome(), objJSONPersistente.getInt(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Boolean)) {
                objPersistente.setBoolean(atributo.getNome(), objJSONPersistente.getBoolean(atributo.getNome()));
            } else if (tipoAtributo.equals(Atributo.Time)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()), "HH:mm");
                objPersistente.setTime(atributo.getNome(), new java.sql.Time(dt.getTime()));
            } else if (tipoAtributo.equals(Atributo.Date)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()), "dd/MM/yyyy");
                objPersistente.setDate(atributo.getNome(), new java.sql.Date(dt.getTime()));
            } else if (tipoAtributo.equals(Atributo.TimeStamp)) {
                java.util.Date dt = br.com.rosena.util.Util.stringToDate(objJSONPersistente.getString(atributo.getNome()), "HH:mm:ss");
                objPersistente.setTime(atributo.getNome(), new java.sql.Time(dt.getTime()));
            } else if (tipoAtributo.contains(Atributo.XML)) {
                if (!atributo.getConsultaDoAtributo().equals(Atributo.XML_LIST)) {
                    if (objJSONPersistente.getLong(atributo.getNome()) > 0) {
                        objPersistente.setPersistente(atributo.getNome(), new Persistente(atributo.getTipo().replace(".xml", ""), objJSONPersistente.getLong(atributo.getNome())));
                    }
                }
            }
        }
        if (objPersistente.getId() > 0) {
            objJSONPersistente.put("id", objPersistente.getId());
        } else {
            objJSONPersistente.put("id", 0);
        }
        objJSONPersistente.put("error", ex.getMessage().replace("java.lang.Exception:", ""));//incluir error
        out.print("<script> alert('" + ex.getMessage().replace("java.lang.Exception:", "") + "'); </script>");
        return;
    }
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload <%=nomeClasse%></title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.fileupload.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#fileUpload").fileUpload({
            'uploader': 'uploader.swf',
            'cancelImg': 'cancel.png',
            'folder': 'upload',
            'script': 'upload.php',
            'scriptData' : '{teste:123}'
        });
        $("#fileUpload2").fileUpload({
            'uploader': 'uploader.swf',
            'height': 30,
            'cancelImg': 'cancel.png',
            'folder': 'upload',
            'script': 'upload.jsp?id=<%=nomeClasse%>_<%=objPersistente.getId()%>',
            'multi': true
        });
        $("#fileUpload3").fileUpload({
            'uploader': 'uploader.swf',
            'cancelImg': 'cancel.png',
            'folder': 'upload',
            'script': 'upload.jsp',
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
            'multi': true,
            'auto': true
        });
    });
</script>
<style>
body {
	font: 12px/18px Arial, Helvetica, sans-serif;
}
.fileUploadQueueItem {
	font: 11px Verdana, Geneva, sans-serif;
	background-color: #F5F5F5;
	border: 3px solid #E5E5E5;
	margin-top: 5px;
	padding: 10px;
	width: 300px;
}
.fileUploadQueueItem .cancel {
	float: right;
}
.fileUploadProgress {
	background-color: #FFFFFF;
	border-top: 1px solid #808080;
	border-left: 1px solid #808080;
	border-right: 1px solid #C5C5C5;
	border-bottom: 1px solid #C5C5C5;
	margin-top: 10px;
	width: 100%;
}
.fileUploadProgressBar {
	background-color: #0099FF;
}
</style>
</head>
<body>
<h2 align="center">Envio de Multiplos arquivos</h2>
<p align="center">
	<input name="fileUpload2" id="fileUpload2"  value="Procurar" type="file" />
    <input type="button" class="" onclick="javascript:$('#fileUpload2').fileUploadStart();document.close();" value="Publicar">
    <input type="button" class="" onclick="javascript:$('#fileUpload2').fileUploadClearQueue();" value="Limpar">
</p>
<p>&nbsp;</p>
</body>
</html>